﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using ARR.Entities;
using ARR.UI.Views;
using ARR.Shared;
using System.ComponentModel;
using DDF.Entity;
using ARR.UI.CountryService;
using ARR.UI.StoresService;
using System.ServiceModel;

namespace ARR.UI.Models
{
    public interface IStoreModel : IModelBase
    {
        ObservableCollection<Store> Stores { get; set; }

        ObservableCollection<Country> Countries { get; set; }


        void DeleteStore(List<Store> selectedStores);

        void Save();

        void LoadStores();
    }


    public class StoreModel : ModelBase, IStoreModel
    {
        public StoreModel()
        {
            Countries = new ObservableCollection<Country>();
            Stores = new ObservableCollection<Store>();

            LoadCountries();
            LoadStores();
        }


        public ObservableCollection<Store> Stores { get; set; }

        public ObservableCollection<Country> Countries { get; set; }


        public void DeleteStore(List<Store> selectedStores)
        {
            using (StoreServiceClient service = new StoreServiceClient())
            {
                service.DeleteStores(selectedStores);
            }
        }

        public void Save()
        {
            var toSave = from st in Stores
                         where st.RowState != EntityState.Original
                         select st;

            using (StoreServiceClient service = new StoreServiceClient())
            {
                service.Save(toSave.ToList());
            }
        }

        public void LoadStores()
        {
            using (StoreServiceClient service = new StoreServiceClient())
            {
                var allStores = service.GetAll();
                Stores = new ObservableCollection<Store>(allStores);
            }

            foreach (Store st in Stores)
            {
                st.ChangeRowState(EntityState.Original);
            }
        }

        private void LoadCountries()
        {
            using (CountryServiceClient service = new CountryServiceClient())
            {
                var allCountries = service.GetAll();
                Countries = new ObservableCollection<Country>(allCountries);
            }
        }
    }


    public class StoreModelMock : MockModelBase, IStoreModel
    {
        public StoreModelMock()
        {
            Countries = new ObservableCollection<Country>();
            Stores = new ObservableCollection<Store>();

            LoadCountries();
            LoadStores();
        }


        public ObservableCollection<Store> Stores { get; set; }

        public ObservableCollection<Country> Countries { get; set; }


        public void DeleteStore(List<Store> selectedStores)
        {
            int[] ids = selectedStores.Select(st => st.Id).ToArray();

            foreach (int id in ids)
            {
                Store delete = Stores.SingleOrDefault(st => st.Id == id);

                if (delete != null)
                    Stores.Remove(delete);
            }
        }

        public void Save()
        {
            var original = from st in Stores
                           where st.RowState == EntityState.Original
                           select st;

            var news = from st in Stores
                       where st.RowState == EntityState.New
                       select st;

            var modified = from st in Stores
                           where st.RowState == EntityState.Modified
                           select st;

            original = original.ToList();
            news = news.ToList();
            modified = modified.ToList();
        }

        public void LoadStores()
        {
            Stores.Clear();

            Stores.Add(new Store { Id = 1, Code = "ST1", Name = "Store1", Address = "Address", City = "City", Region = "Region", Phone = "Phone", Fax = "Fax", Format = "Format", Country = new Country { Id = 3, Code = "C3", Name = "Country3" }, CountryId = 3 });
            Stores.Add(new Store { Id = 2, Code = "ST2", Name = "Store2", Address = "Address", City = "City", Region = "Region", Phone = "Phone", Fax = "Fax", Format = "Format", Country = new Country { Id = 3, Code = "C3", Name = "Country3" }, CountryId = 3 });
            Stores.Add(new Store { Id = 3, Code = "ST3", Name = "Store3", Address = "Address", City = "City", Region = "Region", Phone = "Phone", Fax = "Fax", Format = "Format", Country = new Country { Id = 3, Code = "C3", Name = "Country3" }, CountryId = 3 });
            Stores.Add(new Store { Id = 4, Code = "ST4", Name = "Store4", Address = "Address", City = "City", Region = "Region", Phone = "Phone", Fax = "Fax", Format = "Format", Country = new Country { Id = 3, Code = "C3", Name = "Country3" }, CountryId = 3 });
            Stores.Add(new Store { Id = 5, Code = "ST2", Name = "Store5", Address = "Address", City = "City", Region = "Region", Phone = "Phone", Fax = "Fax", Format = "Format", Country = new Country { Id = 3, Code = "C3", Name = "Country3" }, CountryId = 3 });
            Stores.Add(new Store { Id = 6, Code = "ST6", Name = "Store6", Address = "Address", City = "City", Region = "Region", Phone = "Phone", Fax = "Fax", Format = "Format", Country = new Country { Id = 3, Code = "C3", Name = "Country3" }, CountryId = 3 });

            foreach (Store st in Stores)
            {
                st.ChangeRowState(EntityState.Original);
            }

        }

        private void LoadCountries()
        {
            Countries.Add(new Country { Id = 1, Code = "C1", Name = "Country1" });
            Countries.Add(new Country { Id = 2, Code = "C2", Name = "Country2" });
            Countries.Add(new Country { Id = 3, Code = "C3", Name = "Country3" });
            Countries.Add(new Country { Id = 22, Code = "C4", Name = "Country4" });
        }
    }
}
